package cn.gailvlun.gll.presentation.emotion.photo

import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.annotation.Nullable
import android.support.v4.app.ActivityCompat
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import cn.gailvlun.gll.R
import cn.gailvlun.gll.databinding.ActivityGalleryBinding
import cn.gailvlun.gll.presentation.base.BaseActivity


const val EXTRA_PHOTO_URI_LIST = "extra_photo_uri_list"
const val EXTRA_START_POSITION = "extra_start_position"

class GalleryActivity : BaseActivity() {

    companion object {
        fun openActivity(activity: BaseActivity, photoUriList: ArrayList<String>, position: Int, @Nullable transBundle: Bundle?) {
            val intent = Intent(activity, GalleryActivity::class.java)
            intent.putStringArrayListExtra(EXTRA_PHOTO_URI_LIST, photoUriList)
            intent.putExtra(EXTRA_START_POSITION, position)
            ActivityCompat.startActivity(activity, intent, transBundle)

        }
    }

    private var mPhotoUriList: MutableList<String>? = null
    private var mStartPosition: Int = -1
    private lateinit var mBinding: ActivityGalleryBinding
    override fun initComponent() {










        mPhotoUriList = intent.getStringArrayListExtra(EXTRA_PHOTO_URI_LIST)
        mStartPosition = intent.getIntExtra(EXTRA_START_POSITION, 0)

        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_gallery)
        mBinding.vp.adapter = VpAdapter(supportFragmentManager, mPhotoUriList)
        mBinding.vp.currentItem = mStartPosition
        mBinding.vp.offscreenPageLimit = mPhotoUriList?.size!!

    }

    override fun createHandler() {
    }

    override fun loadData(savedInstanceState: Bundle?) {
    }


    inner class VpAdapter(
            fm: FragmentManager?,
            private var mPhotoUriList: MutableList<String>?)
        : FragmentStatePagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            return GalleryFragment.newInstance(mPhotoUriList!![position], mPhotoUriList!![position], position)
        }

        override fun getCount(): Int {
            return mPhotoUriList?.size!!
        }
    }
}